package com.andrew.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.andrew.dao.BookDao;
import com.andrew.dao.CustomerDao;
import com.andrew.dao.OrderDao;
import com.andrew.dao.impl.BookDaoImpl;
import com.andrew.dao.impl.CustomerDaoImpl;
import com.andrew.dao.impl.OrderDaoImpl;
import com.andrew.entity.Book;
import com.andrew.entity.Customer;
import com.andrew.entity.Order;
import com.andrew.entity.OrderDetail;
import com.andrew.service.OrderService;
import com.andrew.service.impl.OrderServiceImpl;

public class InsertOrderByBookIdServlet extends HttpServlet {
	private OrderService orderService=new OrderServiceImpl();
	private BookDao bookdao=new BookDaoImpl();
	private OrderDao orderDao=new OrderDaoImpl();
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		
		/*//测试
			
			try {
				Customer customer = new CustomerDaoImpl().findCustomer(1);
				req.getSession().setAttribute("customer", customer);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}	
		//end*/
		
		//获取书本对象
		String bookId=req.getParameter("bookId");
		Book book=null;
		try {
			book = bookdao.queryBookbyBookId(Integer.parseInt(bookId));
		} catch (NumberFormatException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//获取当前用户
		Customer customer=(Customer)req.getSession().getAttribute("customer");
		//创建vc
		Random random=new Random();
		String vc=String.valueOf(random.nextInt(999999-100000+1)+100000);
		//创建订单对象
		Order order=new Order();
		//设置普通属性
		order.setAddress("not set");
		order.setCreateDate(sdf.format(new Date()));
		order.setCustomer(customer);
		order.setOrderId(String.valueOf(System.currentTimeMillis()+customer.getCustomerId()));

		order.setSTATUS(-1);
		order.setTotalPrice(book.getPrice());
		order.setVC(vc);
		//创建订单条目集合
		List<OrderDetail> orderDetailsList=new ArrayList<OrderDetail>();
		//设置订单条目属性
		OrderDetail od=new OrderDetail();
		od.setBook(book);
		od.setOrder(order);
		od.setOrderDetailId(0);
		od.setSubTotal(book.getPrice());
		od.setQuantity(1);
		od.setSTATUS(0);
		orderDetailsList.add(od);
		//设置订单详情
		order.setOrderDetailsList(orderDetailsList);
		//添加订单
		orderService.addOrder(order);
		
		//保存至request域，并转发地址
		req.setAttribute("order", order);
		req.getRequestDispatcher("order_payment.jsp").forward(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(req, resp);
	}
}
